/**
 *
 * Copyright (C), 2002-2017, è½¦å›½ç½‘.
 */
package main;

import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;



/**
 *
 * @author zhangweifeng
 * @version $Id: ExampleTargetPlugin.java, v 0.1 2017年11月23日 下午7:40:26 zhangweifeng Exp $
 */
public class ExampleTargetPlugin extends PluginAdapter{
	
	public static final String PRO_TARGET_PACKAGE = "targetPackage";  // 配置targetPackage名
    private static String targetPackage;   // 目标包

	/** 
	 * @param arg0
	 * @return
	 * @see org.mybatis.generator.api.Plugin#validate(java.util.List)
	 */
	@Override
	public boolean validate(List<String> arg0) {
		// 获取配置的目标package
        Properties properties = getProperties();
        targetPackage = properties.getProperty(PRO_TARGET_PACKAGE);
        if (targetPackage == null){
            System.out.println("请配置main.ExampleTargetPlugin插件的目标包名(targetPackage)！");
            return false;
        }
        return true;
	}
	
	/** 
	 * @param introspectedTable
	 * @see org.mybatis.generator.api.PluginAdapter#initialized(org.mybatis.generator.api.IntrospectedTable)
	 */
	@Override
	public void initialized(IntrospectedTable introspectedTable) {
		String exampleType = introspectedTable.getExampleType();
        // 修改包名
        Context context = getContext();
        JavaModelGeneratorConfiguration configuration = context.getJavaModelGeneratorConfiguration();
        String targetPackage = configuration.getTargetPackage();
        String newExampleType = exampleType.replace(targetPackage, this.targetPackage);

        introspectedTable.setExampleType(newExampleType);

        System.out.println("[Example 目标包修改插件]:修改"+introspectedTable.getExampleType()+"的包到"+this.targetPackage);
	}

}
